﻿$(function () {

    commTemplUtil.initPage();

    $.commNavTabs2($("#tab-nav-1").find(".tab-head"),
        $("#tab-nav-1").find(".tab-supply-ul"),
        "active");

    $.commNavTabs2($("#tab-nav-2").find(".item"),
        $("#tab-nav-2").find(".info-content-date"),
        "current");

    createCharts();
    //首页 最上面的切换

    $(".search-type > .item").click(function () {
        $(".search-type > .item").attr("class", "item");
        $(this).attr("class", "item f-coffee");
        $("#roleid").attr("value", "");
        $("#roleid").attr("value", "" + $(this).attr("id") + "")

    });
    clickSearch();
    //

    //登录状态的才操作
    var getLoginState = COMM_DATA.topObj.loginFlag;
    if (getLoginState == "1") {
        //弹出框
        var getSo = COMM_DATA.webObj.createid;
        if (getSo == "1") {
            $(".info-left").click(function () {
                location.href = "" + COMM_DATA.topObj.issue1_href + "";
            });
            $(".info-right").click(function () {
                alert("身份验证错误，没有权限");
            });
        } else {
            $(".info-right").click(function () {
                location.href = "" + COMM_DATA.topObj.issue2_href + "";
            });
            $(".info-left").click(function () {
                alert("身份验证错误，没有权限");
            });
        }
        createDialog();
        createDialog1();
    } else {
        $(".info-left").click(function () {
            location.href = "" + COMM_DATA.topObj.login_href + "";
        });
        $(".info-right").click(function () {
            location.href = "" + COMM_DATA.topObj.login_href + "";
        });
        $(".sel-supply-btn").click(function () {
            location.href = "" + COMM_DATA.topObj.login_href + "";
        });
        $(".product-enquiry1").click(function () {
            location.href = "" + COMM_DATA.topObj.login_href + "";
        });
    }

    // 询价找车,验证规则.
    var myFormValiObj = $("#myform").validate({
            rules : {
                loadpos : "required",
                unloadpos : "required",
                goodsdesc : "required",
                quantity : {
                    required : true,
                    digits : true
                },
                tel : {
                    required : true,
                    mobileCHINA : true,
                }

            },
            messages : {
                quantity : {
                    digits : "请输入整数"
                },
                tel : {
                    mobileCHINA : "请输入正确格式的手机号码",
                }
            }

        });

    // 帮我找车
    $("#find-truck-btn").on("click", function () {

        if (!COMM_DATA.topObj.userid) {
            // 未登录.
            var conf = {
                title : "注意",
                message : "请先登录!",
                ok_txt : "确定"
            };
            commTemplUtil.showErrorDialog(conf);
            return false;
        }

        var flag = myFormValiObj.form();

        var formObj = $("#myform");

        if (!flag) {
            // 验证失败
            return false;
        }

        var loadposVal = formObj.find("input[name='loadpos']").val() || '';
        var unloadposVal = formObj.find("input[name='unloadpos']").val() || '';
        var goodsdescVal = formObj.find("input[name='goodsdesc']").val() || '';
        var quantityVal = formObj.find("input[name='quantity']").val() || '';
        var telVal = formObj.find("input[name='tel']").val() || '';

        $.ajax({
            url : PAGE_CONFIG.requestUrl.addFindTruck,
            type : 'post',
            dataType : 'json',
            data : {
                loadpos : loadposVal, // 装车点
                unloadpos : unloadposVal, // 卸货点
                goodsdesc : goodsdescVal, // 货物名称
                quantity : quantityVal, // 数量
                tel : telVal, // 联系电话
                _token : COMM_DATA.csrfToken,
            },
            success : function (result) {
                var conf = {
                    title : "提示",
                    message : "提交成功!",
                    ok_txt : "确定"
                };
                commTemplUtil.showErrorDialog(conf);

            },
            error : function (result) {
                var conf = {
                    title : "提示",
                    message : "提交失败!",
                    ok_txt : "确定"
                };
                commTemplUtil.showErrorDialog(conf);
            }
        });

    });

});

function createCharts() {
    require.config({
        paths : {
            echarts : './temp-resouce/js'
        }
    });

    require(
        [
            'echarts',
            'echarts/chart/line',
            'echarts/chart/k'
        ],
        function (ec) {

        //
        var myChart = ec.init(document.getElementById('main-price'));
        myChart.setOption({
            tooltip : {
                trigger : 'axis'
            },
            legend : {
                data : []//['OBV', 'OBV_MA']
            },
            toolbox : {
                show : false,
                feature : {
                    mark : {
                        show : true
                    },
                    dataView : {
                        show : true,
                        readOnly : false
                    },
                    restore : {
                        show : true
                    },
                    saveAsImage : {
                        show : true
                    }
                }
            },
            xAxis : [{
                    type : 'category',
                    data : ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
                }
            ],
            yAxis : [{
                    type : 'value',
                    splitArea : {
                        show : true
                    }
                }
            ],
            series : [{
                    name : '',
                    type : 'line',
                    data : [85632, 85586, 85544, 85532, 85480, 85450, 85400, 85460, 85430, 85490, 85550, 85320]
                }, {
                    name : '',
                    type : 'line',
                    data : [85550, 85540, 85556, 85550, 85500, 85462, 85432, 85402, 85352, 85302, 85325, 85320]
                }
            ]
        });

        //
        var myChart = ec.init(document.getElementById('internationtrade'));
        myChart.setOption({
            tooltip : {
                trigger : 'axis'
            },
            legend : {
                data : ['', '']
            },
            toolbox : {
                show : false,
                feature : {
                    mark : {
                        show : true
                    },
                    dataView : {
                        show : true,
                        readOnly : false
                    },
                    restore : {
                        show : true
                    },
                    saveAsImage : {
                        show : true
                    }
                }
            },
            xAxis : [{
                    type : 'category',
                    data : ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
                }
            ],
            yAxis : [{
                    type : 'value',
                    splitArea : {
                        show : true
                    }
                }
            ],
            series : [{
                    name : 'OBV',
                    type : 'line',
                    data : [85632, 85586, 85544, 85532, 85480, 85450, 85400, 85460, 85430, 85490, 85550, 85320]
                }, {
                    name : 'OBV_MA',
                    type : 'line',
                    data : [85550, 85540, 85556, 85550, 85500, 85462, 85432, 85402, 85352, 85302, 85325, 85320]
                }
            ]
        });

        //
        var domesticChart = ec.init(document.getElementById('domestictrade'));
        domesticChart.setOption({
            title : {
                text : ''
            },
            tooltip : {
                trigger : 'axis',
                formatter : function (params) {
                    var res = params[0].seriesName + ' ' + params[0].name;
                    res += '<br/>  开盘 : ' + params[0].value[0] + '  最高 : ' + params[0].value[3];
                    res += '<br/>  收盘 : ' + params[0].value[1] + '  最低 : ' + params[0].value[2];
                    return res;
                }
            },
            legend : {
                data : ['']
            },
            toolbox : {
                show : false,
                feature : {
                    mark : {
                        show : true
                    },
                    dataZoom : {
                        show : true
                    },
                    dataView : {
                        show : true,
                        readOnly : false
                    },
                    magicType : {
                        show : true,
                        type : ['line', 'bar']
                    },
                    restore : {
                        show : true
                    },
                    saveAsImage : {
                        show : true
                    }
                }
            },
            dataZoom : {
                show : true,
                realtime : true,
                start : 50,
                end : 100
            },
            xAxis : [{
                    type : 'category',
                    boundaryGap : true,
                    axisTick : {
                        onGap : false
                    },
                    splitLine : {
                        show : false
                    },
                    data : [
                        "2014/2/28", "2014/3/7", "2014/3/14", "2014/3/21", "2014/3/28",
                        "2014/4/4", "2014/4/11", "2014/4/18", "2014/4/25", "2014/4/30",
                        "2014/5/9", "2014/5/16", "2014/5/23", "2014/5/30", "2014/6/6",
                        "2014/6/13", "2014/6/20", "2014/6/27", "2014/7/4", "2014/7/11",
                        "2014/7/18", "2014/7/25", "2014/8/1", "2014/8/8", "2014/8/15",
                        "2014/8/22", "2014/8/29", "2014/9/5", "2014/9/12", "2014/9/19",
                        "2014/9/26", "2014/10/10", "2014/10/17", "2014/10/24", "2014/10/31",
                        "2014/11/7", "2014/11/14", "2014/11/21", "2014/11/28", "2014/12/5",
                        "2014/12/12", "2014/12/19", "2014/12/26", "2014/12/31", "2015/1/9",
                        "2015/1/16", "2015/1/23", "2015/1/30", "2015/2/6", "2015/2/13",
                        "2015/2/27", "2015/3/6", "2015/3/13", "2015/3/20", "2015/3/27",
                        "2015/4/3", "2015/4/10", "2015/4/17", "2015/4/24", "2015/4/30",
                        "2015/5/8", "2015/5/15", "2015/5/22", "2015/5/29", "2015/6/5",
                        "2015/6/12", "2015/6/19", "2015/6/26", "2015/7/3", "2015/7/10",
                        "2015/7/17", "2015/7/24", "2015/7/31", "2015/8/7", "2015/8/14",
                        "2015/8/21", "2015/8/28", "2015/9/2", "2015/9/11", "2015/9/18",
                        "2015/5/30", "2015/5/31", "2015/6/3", "2015/6/4", "2015/6/5",
                        "2015/9/25", "2015/10/9", "2015/10/16"
                    ]
                }
            ],
            yAxis : [{
                    type : 'value',
                    scale : true,
                    boundaryGap : [0.01, 0.01]
                }
            ],
            series : [{
                    name : '软商品期货指数',
                    type : 'k',
                    data : [// 开盘，收盘，最低，最高
                        [767.29, 763.16, 760.16, 767.89],
                        [725.29, 743.16, 720.16, 743.89],
                        [735.29, 756.16, 734.16, 773.89],
                        [734.29, 728.16, 734.16, 728.89],
                        [770.29, 753.16, 771.16, 752.89],
                        [770.29, 753.16, 771.16, 752.89],
                        [767.29, 763.16, 760.16, 767.89],
                        [771.29, 793.16, 793.16, 798.89],
                        [796.29, 786.16, 796.16, 786.89],
                        [792.29, 799.16, 792.16, 805.89],
                        [734.29, 728.16, 734.16, 728.89],
                        [746.29, 797.16, 746.16, 783.89],
                        [788.29, 787.16, 785.16, 787.89],
                        [770.29, 753.16, 771.16, 752.89],
                        [770.29, 753.16, 771.16, 752.89],
                        [767.29, 763.16, 760.16, 767.89],
                        [725.29, 743.16, 720.16, 743.89],
                        [735.29, 756.16, 734.16, 773.89],
                        [734.29, 728.16, 734.16, 728.89],
                        [746.29, 797.16, 746.16, 783.89],
                        [788.29, 787.16, 785.16, 787.89],
                        [770.29, 753.16, 771.16, 752.89],
                        [770.29, 753.16, 771.16, 752.89],
                        [767.29, 763.16, 760.16, 767.89],
                        [771.29, 793.16, 793.16, 798.89],
                        [796.29, 786.16, 796.16, 786.89],
                        [792.29, 799.16, 792.16, 805.89],
                        [799.29, 809.16, 794.16, 805.89],
                        [734.29, 728.16, 734.16, 728.89],
                        [725.29, 743.16, 720.16, 743.89],
                        [735.29, 756.16, 734.16, 773.89],
                        [734.29, 728.16, 734.16, 728.89],
                        [746.29, 797.16, 746.16, 783.89],
                        [771.29, 793.16, 793.16, 798.89],
                        [796.29, 786.16, 796.16, 786.89],
                        [805.29, 793.16, 793.16, 805.89],
                        [770.29, 753.16, 771.16, 752.89],
                        [767.29, 763.16, 760.16, 767.89],
                        [771.29, 793.16, 793.16, 798.89],
                        [792.29, 799.16, 792.16, 805.89],
                        [799.29, 809.16, 794.16, 805.89],
                        [734.29, 728.16, 734.16, 728.89],
                        [725.29, 743.16, 720.16, 743.89],
                        [735.29, 756.16, 734.16, 773.89],
                        [734.29, 728.16, 734.16, 728.89],
                        [746.29, 797.16, 746.16, 783.89],
                        [788.29, 787.16, 785.16, 787.89],
                        [770.29, 753.16, 771.16, 752.89],
                        [767.29, 763.16, 760.16, 767.89],
                        [771.29, 793.16, 793.16, 798.89],
                        [796.29, 786.16, 796.16, 786.89],
                        [805.29, 793.16, 793.16, 805.89],
                        [770.29, 753.16, 771.16, 752.89],
                        [767.29, 763.16, 760.16, 767.89],
                        [771.29, 793.16, 793.16, 798.89],
                        [796.29, 786.16, 796.16, 786.89],
                        [792.29, 799.16, 792.16, 805.89],
                        [799.29, 809.16, 794.16, 805.89],
                        [796.29, 786.16, 796.16, 786.89],
                        [792.29, 799.16, 792.16, 805.89],
                        [799.29, 809.16, 794.16, 805.89],
                        [734.29, 728.16, 734.16, 728.89],
                        [725.29, 743.16, 720.16, 743.89],
                        [735.29, 756.16, 734.16, 773.89],
                        [734.29, 728.16, 734.16, 728.89],
                        [725.29, 743.16, 720.16, 743.89],
                        [735.29, 756.16, 734.16, 773.89],
                        [746.29, 797.16, 746.16, 783.89],
                        [788.29, 787.16, 785.16, 787.89],
                        [770.29, 753.16, 771.16, 752.89],
                        [770.29, 753.16, 771.16, 752.89],
                        [767.29, 763.16, 760.16, 767.89],
                        [771.29, 793.16, 793.16, 798.89],
                        [796.29, 786.16, 796.16, 786.89],
                        [792.29, 799.16, 792.16, 805.89],
                        [799.29, 809.16, 794.16, 805.89],
                        [734.29, 728.16, 734.16, 728.89],
                        [725.29, 743.16, 720.16, 743.89],
                        [735.29, 756.16, 734.16, 773.89],
                        [746.29, 797.16, 746.16, 783.89],
                        [788.29, 787.16, 785.16, 787.89],
                        [770.29, 753.16, 771.16, 752.89],
                        [767.29, 763.16, 760.16, 767.89],
                        [771.29, 793.16, 793.16, 798.89],
                        [796.29, 786.16, 796.16, 786.89],
                        [792.29, 799.16, 792.16, 805.89],
                        [799.29, 809.16, 794.16, 805.89],
                        [805.29, 793.16, 793.16, 805.89]
                    ]
                }
            ]
        });
    });

};

//弹出框的加载询盘
function createDialog() {
    var tmplObj = _.template($('#supply_offer_templ').html());
    var tmplObjHtml = tmplObj();
    $(".sel-supply-btn").click(function () {
        var getCode = $(this).attr("code");
        var getMax = $(this).attr("maxSupply");
        var dia = dialog({
                id : 'offer-info',
                title : ' ',
                content : tmplObjHtml,
                onshow : function () {
                    //弹出框的验证
                    $("[id='content:offer-info']").find("#supper_offer_sub").validate({
                        rules : {
                            price : {
                                required : true,
                                number : true,
                            },
                            amount : {
                                required : true,
                                digits : true,
                            },
                            contact_name : "required",
                            contact_phone : {
                                required : true,
                                mobileCHINA : true,
                            },
                            county_id : "required",
                        },
                        messages : { //
                            contact_phone : {
                                required : "请输入手机号码",
                                mobileCHINA : "请输入有效手机号码"
                            },
                            amount : {
                                digits : "只能输入整数",
                            },
                        },
                        errorPlacement : function (error, element) {
                            if (element.attr("name") == "signflag") {
                                element.parents(".electronic-bottom").append(error);
                            } else {
                                element.parents(".enquiry").after($("<div class='key-lbl tc' style='width:auto;'/>").append(error));
                            }
                        },

                    });
                }
            });
        dia.showModal();
        $("#enquiry_id").attr("value", getCode);
        $("#max_enquiry").attr("value", getMax);
        //弹出框 地区的加载
        var addrTemplObj = _.template($('#addr-option-templ').html());
        renderPage();

        function renderPage() {
            // 省
            $.ajax({
                url : PAGE_CONFIG.requestUrl.addrProvince,
                type : 'get',
                dataType : 'json',
                success : function (result) {
                    var datas = $.extend([], result.data);
                    var tmplObjHtml = addrTemplObj(datas);
                    $("#province").html(tmplObjHtml);
                },
                error : function (xhr, status, error) {
                    var tmplObjHtml = addrTemplObj([]);
                    $("#province").html(tmplObjHtml);
                }
            });

            $("#province").on("change", function () {
                var code = $(this).val();
                renderAddrCity(code);
            });

            $("#city").on("change", function () {
                var code = $(this).val();
                renderAddrCounty(code);
            });

            function renderAddrCity(provinceCode) {

                // 市
                $.ajax({
                    url : PAGE_CONFIG.requestUrl.addrCity,
                    type : 'post',
                    dataType : 'json',
                    data : {
                        code : provinceCode,
                        _token : COMM_DATA.csrfToken,
                    },
                    success : function (result) {
                        var datas = $.extend([], result.data);
                        var tmplObjHtml = addrTemplObj(datas);
                        $("#city").html(tmplObjHtml);

                    },
                    error : function (xhr, status, error) {
                        var tmplObjHtml = addrTemplObj([]);
                        $("#city").html(tmplObjHtml);
                    }
                });

                $("#city").empty().html(addrTemplObj([]));
                $("#county").empty().html(addrTemplObj([]));
            };

            function renderAddrCounty(cityCode) {
                // 区/县
                $.ajax({
                    url : PAGE_CONFIG.requestUrl.addrCounty,
                    type : 'post',
                    dataType : 'json',
                    data : {
                        code : cityCode,
                        _token : COMM_DATA.csrfToken,
                    },
                    success : function (result) {
                        var datas = $.extend([], result.data);
                        var tmplObjHtml = addrTemplObj(datas);
                        $("#county").html(tmplObjHtml);

                    },
                    error : function (xhr, status, error) {
                        var tmplObjHtml = addrTemplObj([]);
                        $("#county").html(tmplObjHtml);
                    }
                });
            };
        };
    });

};

//弹出框的加载报盘
function createDialog1() {
    var tmplObj = _.template($('#supply_offer_templ1').html());
    var tmplObjHtml = tmplObj();
    $(".product-enquiry1").click(function () {
        var getCode = $(this).attr("code");
        var getMax = $(this).attr("maxSupply");
        var dia = dialog({
                id : 'offer-info1',
                title : ' ',
                content : tmplObjHtml,
                onshow : function () {
                    //弹出框的验证
                    $("[id='content:offer-info1']").find("#supper_offer_sub1").validate({
                        rules : {
                            price : {
                                required : true,
                                number : true,
                            },
                            amount : {
                                required : true,
                                digits : true,
                            },
                            contact_name : "required",
                            contact_phone : {
                                required : true,
                                mobileCHINA : true,
                            },
                            county_id : "required",
                        },
                        messages : { //
                            contact_phone : {
                                required : "请输入手机号码",
                                mobileCHINA : "请输入有效手机号码"
                            },
                            amount : {
                                digits : "只能输入整数",
                            },
                        },
                        errorPlacement : function (error, element) {
                            if (element.attr("name") == "signflag") {
                                element.parents(".electronic-bottom").append(error);
                            } else {
                                element.parents(".enquiry1").after($("<div class='key-lbl tc' style='width:auto;'/>").append(error));
                            }
                        },

                    });
                }
            });
        dia.showModal();
        $("#enquiry_id").attr("value", getCode);
        $("#max_enquiry").attr("value", getMax);
        //弹出框 地区的加载
        var addrTemplObj = _.template($('#addr-option-templ').html());
        renderPage();

        function renderPage() {
            // 省
            $.ajax({
                url : PAGE_CONFIG.requestUrl.addrProvince,
                type : 'get',
                dataType : 'json',
                success : function (result) {
                    var datas = $.extend([], result.data);
                    var tmplObjHtml = addrTemplObj(datas);
                    $("#province").html(tmplObjHtml);
                },
                error : function (xhr, status, error) {
                    var tmplObjHtml = addrTemplObj([]);
                    $("#province").html(tmplObjHtml);
                }
            });

            $("#province").on("change", function () {
                var code = $(this).val();
                renderAddrCity(code);
            });

            $("#city").on("change", function () {
                var code = $(this).val();
                renderAddrCounty(code);
            });
            function renderAddrCity(provinceCode) {

                // 市
                $.ajax({
                    url : PAGE_CONFIG.requestUrl.addrCity,
                    type : 'post',
                    dataType : 'json',
                    data : {
                        code : provinceCode,
                        _token : COMM_DATA.csrfToken,
                    },
                    success : function (result) {
                        var datas = $.extend([], result.data);
                        var tmplObjHtml = addrTemplObj(datas);
                        $("#city").html(tmplObjHtml);

                    },
                    error : function (xhr, status, error) {
                        var tmplObjHtml = addrTemplObj([]);
                        $("#city").html(tmplObjHtml);
                    }
                });

                $("#city").empty().html(addrTemplObj([]));
                $("#county").empty().html(addrTemplObj([]));
            };

            function renderAddrCounty(cityCode) {
                // 区/县
                $.ajax({
                    url : PAGE_CONFIG.requestUrl.addrCounty,
                    type : 'post',
                    dataType : 'json',
                    data : {
                        code : cityCode,
                        _token : COMM_DATA.csrfToken,
                    },
                    success : function (result) {
                        var datas = $.extend([], result.data);
                        var tmplObjHtml = addrTemplObj(datas);
                        $("#county").html(tmplObjHtml);
                    },
                    error : function (xhr, status, error) {
                        var tmplObjHtml = addrTemplObj([]);
                        $("#county").html(tmplObjHtml);
                    }
                });
            };
        };
    });

};
//登录状态 的获取
function loginState(className) {
    $("." + className + "").click(function () {
        location.href = "";
    });
};
//点击搜索 的事件

function clickSearch() {
    $(".button").click(function () {
        var getCodeVal = $(".f-coffee").attr("code");
        //获取搜索 输入的文本框 的值
        var getText = $("#search_text").val();

        if (getText == null) {
            alert("请输入关键字");
        }
    });
}